package eclipse.weibo.modle.control;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import eclipse.weibo.util.ReadImageUtil;

import weibo4j.util.DebugPrn;

public class RetweetComposite extends Composite {
    private static DebugPrn dp = new DebugPrn(RetweetComposite.class.getName());
    private static final Cursor cursor = Display.getDefault().getSystemCursor(SWT.CURSOR_HAND);
    private Color backColor = getBackColor();
    private int default_widthHint;
    private Composite parent;
    private String original_pic;
    private Text retweetText = null;
    private Label retweetImage = null;

    public RetweetComposite(Composite parent) {
        this(parent, 240);
    }

    public RetweetComposite(Composite parent, int default_widthHint) {
        super(parent, SWT.WRAP);
        this.parent = parent;
        this.default_widthHint = default_widthHint;
        this.setLayout(new GridLayout(1, false));
        this.setBackground(backColor);
    }

    private Color getBackColor() {
        float[] hsb = this.getBackground().getRGB().getHSB();
        return new Color(Display.getDefault(), new RGB(hsb[0], hsb[1], hsb[2] - (float) 0.5));
    }

    public void setRetweetText(String text) {
        getRetweetText().setText(text);
    }

    public void setRetweetImage(String thumbnail_pic,String original_pic) {
        try {
            this.original_pic = original_pic;
            ImageData image = ReadImageUtil.read(new URL(thumbnail_pic));
            getRetweetImage().setImage(new Image(Display.getDefault(), image));
        } catch (MalformedURLException ex) {
            dp.error(ex, ex);
        }
    }

    private Text getRetweetText() {
        if (retweetText == null) {
            retweetText = new Text(this, SWT.LEFT | SWT.WRAP);
        }
        retweetText.setLayoutData(getRetweetGridData());
        retweetText.setEditable(false);
        retweetText.setBackground(backColor);

        return retweetText;
    }

    private Label getRetweetImage() {
        if (retweetImage == null) {
            retweetImage = new Label(this, SWT.NONE);
        }
        retweetImage.setCursor(cursor);
        retweetImage.addMouseListener(new ImageMouseListener());
        return retweetImage;
    }

    private GridData getRetweetGridData() {
        GridData o1gd2 = new GridData();
        o1gd2.widthHint = default_widthHint;
        return o1gd2;
    }
    private class ImageMouseListener extends MouseAdapter{
        public void mouseDown(MouseEvent e) {
            if(e.button == 1){
                new ImageDialog(parent.getShell(), original_pic).open();
            }
        }
    }
}
